package com.shimao.celnet.payment.service.impl;

import com.github.pagehelper.PageHelper;
import com.shimao.celnet.payment.form.BCtaxGoodsCodeForm;
import com.shimao.celnet.payment.model.BCtaxGoodsCode;
import com.shimao.celnet.payment.service.BCtaxGoodsCodeService;
import com.shimao.celnet.payment.util.FinalUtil;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.Date;
import java.util.List;

/**
 * 国税商品编码管理
 * Created by ensure on 2017/12/3.
 */
@Service
public class BCtaxGoodsCodeServiceImpl extends BaseServiceImpl<BCtaxGoodsCode> implements BCtaxGoodsCodeService {
    protected Logger logger = Logger.getLogger(this.getClass());

    /**
     * 分页查询
     * @param formBean
     * @param pageNum
     * @param length
     * @return
     */
    @Override
    public BCtaxGoodsCodeForm getByPage(BCtaxGoodsCodeForm formBean, int pageNum, int length) {
        logger.info("分页查询--开始");
        //分页查询
        Example example = new Example(BCtaxGoodsCode.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(formBean.getCtaxGoodsCode())) {
            //国税商品编码
            criteria.andLike("ctaxGoodsCode", "%" + formBean.getCtaxGoodsCode() + "%");
        }

        if (StringUtil.isNotEmpty(formBean.getCtaxGoodsName())) {
            //国税商品名称
            criteria.andLike("ctaxGoodsName", "%" + formBean.getCtaxGoodsName() + "%");
        }


        //分页查询
        PageHelper.startPage(pageNum, length);
        List<BCtaxGoodsCode> tableList = getByExample(example);
        formBean.setPageInfo(tableList);
        return  formBean;
    }

    /**
     *新增保存
     * @return
     */
    @Override
    public int save(BCtaxGoodsCode sBean){
        int retCnt=0;
        Date date = new Date();
        String loginUserGuid = FinalUtil.INTER_FACE_USER_ID;
        sBean.setCreatedUserGuid(loginUserGuid);
        sBean.setUpdateDate(date);
        sBean.setUpdateUserGuid(loginUserGuid);
        sBean.setCreatedDate(date);
        sBean.setGuid(getGuid());//guid
        retCnt = super.save(sBean);

        return retCnt;
    }

    /**
     *修改保存
     * @return
     */
    @Override
    public int update(BCtaxGoodsCode sBean){
        String loginUserGuid = (String) SecurityUtils.getSubject().getSession().getAttribute("userSessionId");
        sBean.setUpdateUserGuid(loginUserGuid);
        int retCnt = super.update(sBean);

        return retCnt;
    }

    /**
     *存在更新，不存在新增保存
     * @return
     */
    @Override
    public int saveUpdate(BCtaxGoodsCode sBean){
        int retCnt=0;
        //查询
        Example example = new Example(BCtaxGoodsCode.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(sBean.getCtaxGoodsCode())) {
            //国税商品编码
            criteria.andEqualTo("ctaxGoodsCode",  sBean.getCtaxGoodsCode() );
        }
        List<BCtaxGoodsCode> tableList = getByExample(example);
        if(tableList.size()>0 && tableList.size()==1){
            //存在：更新
            retCnt=this.update(tableList.get(0));
        }else{
            //不存在：新增

            String loginUserGuid = FinalUtil.INTER_FACE_USER_ID;
            sBean.setUpdateUserGuid(loginUserGuid);
            sBean.setCreatedUserGuid(loginUserGuid);
            retCnt= this.save(sBean);
        }
        return retCnt;
    }
}
